//
//  BoardView.m
//  CheckersGame
//
//  Created by  on 11-11-23.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "BoardView.h"
#import "checkers_logic/checkers.h"

@implementation BoardView


@synthesize gameState;

- (id) initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self){
        NSString* imageFileName = [[[NSBundle mainBundle] resourcePath] 
                                   stringByAppendingPathComponent: @"board-texture.jpg"];
        CGDataProviderRef provider = CGDataProviderCreateWithFilename([imageFileName UTF8String]);
        
        boardBackgroundImage = CGImageCreateWithJPEGDataProvider(provider, NULL, YES, kCGRenderingIntentDefault);
        
        CGDataProviderRelease(provider);
    }
    return self;
}

- (void) drawBoardImage {
    CGContextRef c = UIGraphicsGetCurrentContext();
    
    // Draw image
    CGContextDrawImage(c, CGRectMake(0, 0, 320, 320), boardBackgroundImage);
    //CGImageRelease(image);
    CGContextDrawPath(c,kCGPathFill);
}

-(void)drawPieceAt:(CheckersPos*)pos first:(BOOL)first{
    CGContextRef c = UIGraphicsGetCurrentContext();
    if (first)
        CGContextSetRGBFillColor(c,0x80/255.0,0,0,1);
    else
        CGContextSetRGBFillColor(c,0,0,0,1);
    CGFloat size = self.bounds.size.width/8; // Block size on the board
    
    CGContextAddEllipseInRect(c, CGRectMake(pos->y*size, pos->x*size,size,size));
    
    CGContextDrawPath(c,kCGPathFill);
    //CGContextClosePath(c);
   // CGContextDrawPath(c,kCGPathFill);
}

- (void)drawRect:(CGRect)rect
{
    CheckersMove moves[CHECKERS_MOVE_BUFFER];
    int i, movecnt, x, y;
    piece_t piece;
    
    
    [self drawBoardImage];
    CGContextRef c = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(c,0,0,0,.5);
    CGPoint coord = {self.bounds.origin.x,self.bounds.origin.y};
    CGFloat size = self.bounds.size.width/8; // Block size on the board
    
    CGContextBeginPath(c);
    for (int i=1; i<8; ++i) {
        for (CGFloat j=size; coord.x+j<self.bounds.size.width; j+=size*2) {
            CGContextAddRect(c, CGRectMake(coord.x+j,coord.y,size,size));
            CGContextAddRect(c, CGRectMake(coord.x,coord.y+j,size,size));
        }
        coord.x += size;
        coord.y += size;
    }
    
    CGContextClosePath(c);
    CGContextDrawPath(c,kCGPathFill);
 //   CGContextClosePath(c);
  //  CGContextDrawPath(c,kCGPathFill);
    
    
    if (gameState){
        c = UIGraphicsGetCurrentContext();
        CGContextBeginPath(c);
        CGContextSetRGBFillColor(c,0,0.6,0.6,1);
        movecnt = chbGenerateValidMoves([gameState getBoardRef], moves);
        
        if (gameState.pieceIsSelected){
            for (i = 0; i < movecnt; i++){
                if (moves[i].from.x == gameState.selectPosition.x && 
                    moves[i].from.y == gameState.selectPosition.y){
                    x = moves[i].to.y * size;
                    y = 280 - (moves[i].to.x+1) * size;
                }
                
                CGContextAddRect(c, CGRectMake(x, y,size,size));
            }
        }
        /*
        for (i = 0; i < movecnt; i++){
            if (moves[i].from.x == gameState.selectPosition.x && 
                moves[i].from.y == gameState.selectPosition.y){
                x = moves[i].to.y * size;
                y = 280 - (moves[i].to.x+1) * size;
            }
            
            CGContextAddRect(c, CGRectMake(x, y,size,size));
        }*/
      //  CGContextClosePath(c);
        CGContextDrawPath(c,kCGPathFill);
    }
    
    /*
    CheckersPos p = {0,0};
    for (p.y = 0; p.y < 8; p.y ++){
        for (p.x = 0; p.x < 8; p.x++){
            if ( p.x%2 != p.y%2 ) {
                piece = chbGetPiece([checkersState getBoardRef], &p);
                if (piece)
                    [self drawPieceAt: &p first: (piece > 0)]; 
            }
        }
    }*/
    /*
    CGRect r = { {0,0}, {10,10}};
    // Drawing code
    int x, y;
    for (y = 0; y < 300; y+=10){
        for (x = 0; x < 300; x+=10){
            r.origin.x = x;
            r.origin.y = y;
        }
    }
    */
}


@end
